#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:jwt.py
# author:周紫茎
# datetime: 2022/6/30 15:15
# software: PyCharm

"""
This is function description
"""

import jwt
from datetime import datetime, timedelta
from app.setting import site


class JWTClaims():
    def __init__(self, UserID, UserType):
        self.UserID = UserID,
        self.UserType = UserType


def genToken(claim: JWTClaims):
    payload = {
        "exp": datetime.now() + timedelta(seconds=int(site.CONFIG['BASIC']['token_expires'])),
        'UserID': claim.UserID,
        "UserType": claim.UserType,
    }
    encoded_jwt = jwt.encode(payload, site.CONFIG['BASIC']['secret_key'], algorithm='HS256')
    return encoded_jwt


def MakeToken(UserID: int, UserType: str) -> str:
    claim = JWTClaims(UserID=UserID,
                      UserType=UserType)
    return genToken(claim)


def VerifyToken(strToken: str) -> JWTClaims:
    token = jwt.decode(strToken, site.CONFIG['BASIC']['secret_key'], algorithms="HS256")
    claim = JWTClaims(UserID=token['UserID'],
                      UserType=token['UserType'])
    return claim
